package per.turtle.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import per.turtle.common.core.constant.CacheConstants;
import per.turtle.common.core.exception.CustomException;
import per.turtle.common.redis.service.RedisService;
import per.turtle.system.entity.SysDictData;
import per.turtle.system.entity.SysDictType;
import per.turtle.system.mapper.SysDictDataMapper;
import per.turtle.system.mapper.SysDictTypeMapper;
import per.turtle.system.service.ISysDictTypeService;

import java.util.List;

/**
 * 字典 服务层实现
 *
 * @author turtle
 */
@RequiredArgsConstructor
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    private final RedisService redisService;

    private final SysDictDataMapper dictDataMapper;

    @Override
    public boolean checkDictTypeUnique(SysDictType dict) {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StrUtil.isNotBlank(dict.getDictType()), SysDictType::getDictType, dict.getDictType())
               .ne(dict.getDictId() != null, SysDictType::getDictId, dict.getDictId());
        List<SysDictType> infos = baseMapper.selectList(wrapper);
        return infos.isEmpty();
    }

    @Override
    public void deleteDictTypeById(Long dictId) {
        SysDictType dictType = baseMapper.selectById(dictId);
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysDictData::getDictType, dictType.getDictType());
        if (dictDataMapper.selectCount(wrapper) > 0) {
            throw new CustomException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
        }
        int row = baseMapper.deleteById(dictId);
        if (row > 0) {
            redisService.deleteObject(dictType.getDictType());
        }
    }

    @CacheEvict(value = CacheConstants.SYS_DICT_KEY, key = "#dict.dictType")
    @Override
    public int insertDictType(SysDictType dict) {
        return baseMapper.insert(dict);
    }

    @Cacheable(value = CacheConstants.SYS_DICT_KEY, key = "'type_all'")
    @Override
    public List<SysDictType> selectDictTypeAll() {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<SysDictType> selectDictTypeList(SysDictType dictType) {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>(dictType);
        wrapper.getEntity().setDictName(null);
        wrapper.lambda().like(StrUtil.isNotBlank(dictType.getDictName()), SysDictType::getDictName, dictType.getDictName());
        return baseMapper.selectList(wrapper);
    }

    @CacheEvict(value = CacheConstants.SYS_DICT_KEY, key = "#dict.dictType")
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDictType(SysDictType dict) {
        SysDictType oldDict = baseMapper.selectById(dict.getDictId());
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysDictData::getDictType, oldDict.getDictType());
        SysDictData dictData = new SysDictData();
        dictData.setDictType(dict.getDictType());
        dictDataMapper.update(dictData, wrapper);
        return baseMapper.updateById(dict);
    }

}
